/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.apache.pig.pigpen.editor.model;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

import org.apache.pig.impl.logicalLayer.LogicalPlan;
import org.apache.pig.impl.logicalLayer.schema.Schema;
import org.apache.pig.pigpen.editor.editparts.OperatorEditPart;

public class Operator extends QueryComponent {

    public static final String DefaultName = "<operator>";

    private OperatorEditPart editPart; // the edit part for this operator

    private Query query; // the query that this operator is part of

    private List<Link> inputs;
    private List<Link> outputs;
    private String text; // query text
    private String alias; // alias of operator output
    private String name = DefaultName; // name of operator (e.g., COGROUP, LOAD)
    private String error; // error, e.g. a parse error (if any)
    private String exdata; // example data for operator output (if any)
    // private IntermedResult ir; // parsed version of operator
    private LogicalPlan plan;
    private Schema schema; // schema of output (if any)

    public static final String TEXT = "TEXT";
    public static final String NAME = "NAME";
    public static final String ERROR = "ERROR";
    public static final String EXDATA = "EXDATA";
    public static final String ALIAS = "ALIAS";

    public Operator() {
        inputs = new ArrayList<Link>();
        outputs = new ArrayList<Link>();
    }

    public void resetDerivedFields() {
        setName(DefaultName);
        setError(null);
        setExdata(null);
        setAlias(null);
        // setIR(null);
        setPlan(null);
        setSchema(null);
    }

    public void setEditPart(OperatorEditPart editPart) {
        this.editPart = editPart;
    }

    public OperatorEditPart getEditPart() {
        return editPart;
    }

    public void setQuery(Query query) {
        this.query = query;
    }

    public Query getQuery() {
        return query;
    }

    public void addInput(Link input) {
        inputs.add(input);
    }

    public void removeInput(Link input) {
        inputs.remove(input);
    }

    public void removeAllInputs() {
        List<Link> inputs = new LinkedList<Link>(getInputs());
        for (Link input : inputs) {
            input.getSource().removeOutput(input);
            query.removeLink(input);
        }
    }

    public List<Link> getInputs() {
        return inputs;
    }

    public void addOutput(Link output) {
        outputs.add(output);
    }

    public void removeOutput(Link output) {
        outputs.remove(output);
    }

    public void removeAllOutputs() {
        List<Link> outputs = new LinkedList<Link>(getOutputs());
        for (Link output : outputs) {
            output.getTarget().removeInput(output);
            query.removeLink(output);
        }
    }

    public List<Link> getOutputs() {
        return outputs;
    }

    public void reParse() {
        getQuery().reParseOp(this);
    }

    public void setText(String text) {
        String oldText = this.text;
        this.text = text;
        firePropertyChange(TEXT, oldText, this.text);
    }

    public String getText() {
        return text;
    }

    public void setError(String error) {
        String oldError = this.error;
        this.error = error;
        firePropertyChange(ERROR, oldError, this.error);
    }

    public String getError() {
        return error;
    }

    public void setExdata(String exdata) {
        String oldExdata = this.exdata;
        this.exdata = exdata;
        firePropertyChange(EXDATA, oldExdata, this.exdata);
    }

    public String getExdata() {
        return exdata;
    }

    public void setAlias(String alias) {
        String oldAlias = this.alias;
        this.alias = alias;
        firePropertyChange(ALIAS, oldAlias, this.alias);
    }

    public String getAlias() {
        return alias;
    }

    public void setName(String name) {
        String oldName = this.name;
        this.name = name;
        firePropertyChange(NAME, oldName, this.name);
    }

    public String getName() {
        return name;
    }

    public void setPlan(LogicalPlan plan) {
        this.plan = plan;
    }

    /*
     * public IntermedResult getIR() { return ir; }
     */
    public LogicalPlan getPlan() {
        return plan;
    }

    public void setSchema(Schema schema) {
        this.schema = schema;
    }

    public Schema getSchema() {
        return schema;
    }

    public String toString() {
        StringBuffer sb = new StringBuffer();
        if (alias != null)
            sb.append(alias + " = ");
        sb.append(name + " ...");
        return sb.toString();
    }

}
